package DAO;


import entity.FoundRecord;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FoundRecordDAO {
    public void addFoundRecord(FoundRecord foundrecord) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        String sql = "insert into found_record(user_id,item_id,found_time,contact_info,status,created_at) values(?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        ps.setInt(1, foundrecord.getUserId());
        ps.setInt(2, foundrecord.getItemId());
        ps.setString(3, foundrecord.getFoundTime());
        ps.setString(4, foundrecord.getContactInfo());
        ps.setString(5, foundrecord.getStatus());
        ps.setString(6, foundrecord.getCreatedAt());
        int rs = ps.executeUpdate();
        if (rs > 0) {
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        ps.close();
        conn.close();
    }
    public void selectFoundRecord() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select * from found_record ORDER BY id ASC";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println("记录id:" + rs.getInt("id") +
                        " 用户id:" + rs.getInt("user_id") +
                        " 物品id:" + rs.getInt("item_id") +
                        " 拾获时间:" + rs.getString("found_time") +
                        " 联系方式:" + rs.getString("contact_info") +
                        " 认领状态:" + rs.getString("status") +
                        " 创建记录时间:" + rs.getString("created_at"));
            }
        } finally {
            JdbcUtils.closeResource(rs, ps);
            JdbcUtils.releaseConnection(conn);
        }
    }
    public static void deleteFoundRecord(FoundRecord record) throws SQLException {
        if (record == null || record.getId() == null) {
            System.out.println("错误：记录或记录ID不能为null");
            return;
        }
        Connection conn = JdbcUtils.getConnection();
        String sql = "delete from found_record where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, record.getId());
        int rs = ps.executeUpdate();
        if (rs > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败，可能记录不存在");
        }

        ps.close();
        conn.close();
    }
    public static void updateFoundRecord(FoundRecord record) throws SQLException {
        if (record == null || record.getId() == null) {
            System.out.println("错误：记录或记录ID不能为null");
            return;
        }
        Connection conn = JdbcUtils.getConnection();
        String sql = "update found_record set item_id=?,found_time=?,contact_info=?,status=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, record.getItemId());
        ps.setString(2, record.getFoundTime());
        ps.setString(3, record.getContactInfo());
        ps.setString(4, record.getStatus());
        ps.setInt(5, record.getId());

        int rs = ps.executeUpdate();
        if (rs > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败，可能记录不存在");
        }
        ps.close();
        conn.close();
    }
    public static boolean existsData(FoundRecord record) {
        if (record == null || record.getId() == null) {
            return false;
        }
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "select 1 from found_record where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, record.getId());
            rs = ps.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            JdbcUtils.closeResource(rs, ps);
            JdbcUtils.releaseConnection(conn);
        }
    }
    }






